package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.dto.*;
import com.example.entity.vo.CoptnVo;
import com.example.entity.yifei.Coptc;
import com.example.entity.yifei.Coptd;
import com.example.entity.yifei.Coptn;
import com.example.entity.yifei.Copto;
import com.example.mapper.CmsniMapper;
import com.example.mapper.CoptdMapper;
import com.example.mapper.CoptnMapper;
import com.example.mapper.CoptoMapper;
import com.example.service.CmsmqService;
import com.example.service.CoptnService;
import com.example.service.CoptoService;
import com.example.untils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@Api(tags = "出货通知单前端控制器")
@Slf4j
@RequestMapping("/api/Coptn")
public class CoptnController {

    @Resource
    private CoptnService coptnService;
    @Resource
    private CoptoService coptoService;
    @Resource
    private CmsmqService cmsmqService;
    @Resource
    private CoptnMapper coptnMapper;
    @Resource
    private CoptoMapper coptoMapper;
    @Resource
    private CoptdMapper coptdMapper;
    @Resource
    private CmsniMapper cmsniMapper;

    @ApiOperation("出货通知单单头列表 销货单模态框分页查询")
    @GetMapping("/queryIPage/{current}/{size}")
    public Result<Map<String, CoptnVo>> queryIPage(@PathVariable Integer current, @PathVariable Integer size, String code){
        return this.coptnService.queryIPage(current,size,code);
    }

    @ApiOperation("出货通知单分页查询")
    @PostMapping("/queryIPages")
    public Result<Map<String,Coptn>> queryIPages(Integer current,Integer size,@RequestBody CoptnDto coptnDto){
        return this.coptnService.queryIPages(current,size,coptnDto);
    }

    @ApiOperation("通过出货通知单的单别单号查询出货通知单单身信息")
    @GetMapping("/queryList/{to001}/{to002}")
    public Result<List<Copto>> queryList(@PathVariable String to001, @PathVariable String to002){
        return this.coptoService.queryList(to001,to002);
    }

    @ApiOperation("通过出货通知单的单别单号查询出货通知单的单头单身信息")
    @GetMapping("/queryOne/{tn001}/{tn002}")
    public Result<Coptn> queryOne(@PathVariable String tn001, @PathVariable String tn002){
        return this.coptoService.queryOne(tn001,tn002);
    }

    @ApiOperation("通过出货通知单单头信息查询订单信息")
    @PostMapping("/queryCopTc")
    public Result<Map<String,Coptd>> queryCopTc(@RequestBody CoptdDtos coptdDtos){
        return this.coptnService.queryCopTc(coptdDtos);
    }

    @ApiOperation("根据单别获取单号")
    @GetMapping("/selectTc002")
    public Result<String> selectTC002(String tc001) {
        String id = cmsmqService.getIdByType(tc001, "coptn");
        return Result.success(id);
    }

    @ApiOperation("出货通知单新增")
    @PostMapping("/save")
    public Result saveCoptn(@RequestBody Coptn coptn) throws ExecutionException, InterruptedException {
        return this.coptnService.saveCoptn(coptn).get();
    }

    @ApiOperation("出货通知单修改")
    @PostMapping("/updateCopTn")
    public Result updateCopTn(@RequestBody Coptn coptn) {
        return this.coptnService.updateCopTn(coptn);
    }

    @ApiOperation("出货通知单删除")
    @GetMapping("/delete/{tn001}/{tn002}")
    public Result delete(@PathVariable String tn001,@PathVariable String tn002){
        return this.coptnService.deleteCopTn(tn001, tn002);
    }

    @ApiOperation("出货单单头单身全部查询")
    @PostMapping("/queryCopToTnList/{current}/{size}")
    public Result<Map<String,Coptn>> queryCopToTnList(@PathVariable Integer current,@PathVariable Integer size,@RequestBody CoptnDto coptnDto){
        return this.coptnService.queryCopToTnList(current,size,coptnDto);
    }

    @ApiOperation("出货通知单(列印)查询,出货通知单的单头和单身")
    @PostMapping("/queryCopTnToLy")
    public Result<Map<String,Coptn>> queryCopTnToLy(@RequestBody CoptnDtos coptnDtos){
        return this.coptnService.queryCopTnToLy(coptnDtos);
    }


//    @ApiOperation("出货通知单新增 纯后端")
//    @PostMapping("/save")
//    @Transactional
//    public Result saveCoptn(@RequestBody Coptc coptc){
//        try {
//            // 新增出货通知单单头
//            Coptn coptn = new Coptn();
//            coptn.setCOMPANY("ZJHZ");
//            coptn.setCREATE_DATE(TimeUtils.getNowTime());
//            coptn.setFLAG(1);
//            coptn.setCREATOR(coptc.getCreator());
//            coptn.setTN001("ASH");
//            coptn.setTN002(cmsmqService.getIdByType("ASH", "coptn"));
//            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
//            coptn.setTN003(dateFormat.format(new Date()));
//            coptn.setTN004(coptc.getTc004());
//            coptn.setTN005("");
//            coptn.setTN006(coptc.getTc005());
//            coptn.setTN007(coptc.getTc006());
//            coptn.setTN008(coptc.getTc004());
//            coptn.setTN009(coptc.getTc010());
//            coptn.setTN010(coptc.getTc011());
//            coptn.setTN011(coptc.getTc016());
//            coptn.setTN012(coptc.getREMARK());
//            coptn.setTN013(coptc.getTc008());
//            coptn.setTN014(coptc.getTc009());
//            coptn.setTN015(new BigDecimal("0.0"));
//            coptn.setTN016("Y");
//            coptn.setTN017(coptc.getTc007());
//            coptn.setTN018("");
//            coptn.setTN019(new BigDecimal("0.0"));
//            coptn.setTN020(0);
//            coptn.setTN021(coptc.getTc039());
//            coptn.setTN022("");
//            coptn.setTN023(coptc.getTc042());
//            coptn.setTN024("N");
//            coptn.setTN025("");
//            coptn.setTN026("");
//            coptn.setTN027("");
//            coptn.setTN028("");
//            coptn.setTN029(0);
//            coptn.setTN030(coptc.getTc041());
//            coptn.setTN031("");
//            coptn.setTN032("");
//            coptn.setTN033("");
//            coptn.setTN034(coptc.getTc031());
//            coptn.setTN035(new BigDecimal("0.0"));
//            coptn.setTN036(new BigDecimal("0.0"));
//            coptn.setTN037("N");
//            coptn.setTN038(coptc.getTc077());
//
//            this.coptnMapper.insert(coptn);
//
//            // 新增出货通知单单身
//            List<Coptd> coptdList = this.coptdMapper.selectList(new LambdaQueryWrapper<Coptd>().eq(Coptd::getTd001, coptc.getTc001()).eq(Coptd::getTd002, coptc.getTc002()));
//            if (!coptdList.isEmpty()){
//                coptdList.forEach(arr -> {
//                    Copto copto = new Copto();
//                    copto.setCOMPANY("ZJHZ");
//                    copto.setCREATE_DATE(TimeUtils.getNowTime());
//                    copto.setFLAG(1);
//                    copto.setCREATOR(coptc.getCreator());
//                    copto.setTO001(coptn.getTN001());
//                    copto.setTO002(coptn.getTN002());
//                    copto.setTO003("000"+String.valueOf(coptdList.indexOf(arr)+1));
//                    copto.setTO004(arr.getTd001());
//                    copto.setTO005(arr.getTd002());
//                    copto.setTO006(arr.getTd003());
//                    copto.setTO007(arr.getTd004());
//                    copto.setTO008(arr.getTd005());
//                    copto.setTO009(arr.getTd006());
//                    copto.setTO010(arr.getTd010());
//                    copto.setTO011(arr.getTd007());
//                    copto.setTO012(arr.getTd040());
//                    copto.setTO013(arr.getTd041());
//                    copto.setTO014("");
//                    copto.setTO015("");
//                    copto.setTO016("");
//                    copto.setTO017(0.000000);
//                    copto.setTO018("");
//                    copto.setTO019(0.000000);
//                    copto.setTO020(arr.getTd011());
//                    copto.setTO021(arr.getTd012());
//                    copto.setTO022(arr.getTd020());
//                    copto.setTO023("N");
//                    copto.setTO024(arr.getTd008());
//                    copto.setTO025(arr.getTd024());
//                    copto.setTO026(arr.getTd014());
//                    copto.setTO027(arr.getTd026());
//                    copto.setTO028(arr.getTd032());
//                    copto.setTO029(arr.getTd034());
//                    copto.setTO030(arr.getTd032());
//                    copto.setTO031(arr.getTd034());
//                    copto.setTO032(arr.getTd036());
//                    copto.setTO033(arr.getTd023());
//                    copto.setTO034(arr.getTd037());
//                    copto.setTO035(arr.getTd038());
//                    copto.setTO036(arr.getTd039());
//                    copto.setTO037("");
//                    copto.setTO038("");
//                    String s = this.cmsniMapper.queryNI002String(arr.getTd007());
//                    if (s!=null){
//                        copto.setTO039(s);
//                    }else {
//                        copto.setTO039("");
//                    }
//                    copto.setTO046(coptc.getTc077());
//                    copto.setTO047("N");
//                    this.coptoMapper.insert(copto);
//                });
//            }
//            return Result.success("出货通知单新增成功");
//        }catch (Exception e){
//            log.error("自动新增出货通知单发生异常"+e.getMessage());
//            throw new YiFeiException(500,"自动新增出货通知单发生异常"+e.getMessage());
//        }
//    }
}
